package com.neteast.androidclient.contact.activity;

import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.neteast.androidclient.contact.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.Time;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class VerificationPhoneActivity extends Activity implements
		OnClickListener {
	private SharedPreferences sharedPreferences;
	private EditText phoneNum, verificationCode;
	private Button btnGetCode, btnLogin;
	private Timer timer;
	private int waitTime = 10;
	private final int UPDATE_TIME = 1;
	private final String SP_NAME = "verification_info";
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UPDATE_TIME:
				btnGetCode.setText(waitTime + "秒后可重发");
				waitTime--;
				if (waitTime <= 0) {
					waitTime = 10;
					btnGetCode.setText("");
					btnGetCode.setEnabled(true);
					btnGetCode
							.setBackgroundResource(R.drawable.btn_get_code_bg);
					timer.cancel();
					timer = null;
				}
				break;

			}
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.verification_phone);
		findView();
		sharedPreferences = getSharedPreferences(SP_NAME, Activity.MODE_PRIVATE);
		if (sharedPreferences.getBoolean("isVerificated", false)) {
			startActivity(new Intent(VerificationPhoneActivity.this,
					MainActivity.class));
			finish();
		}

	}

	private void findView() {
		phoneNum = (EditText) findViewById(R.id.phoneEdt);
		verificationCode = (EditText) findViewById(R.id.codeEdt);
		btnGetCode = (Button) findViewById(R.id.btnGetCode);
		btnLogin = (Button) findViewById(R.id.btnLogin);
		btnGetCode.setOnClickListener(this);
		btnLogin.setOnClickListener(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnGetCode: {
			String num = phoneNum.getText().toString().trim();
			if (num != null && !num.equals("")) {
					// TODO 通知服务器发送验证码到手机
					v.setEnabled(false);
					v.setBackgroundResource(R.drawable.btn_get_code_disabled);
					if (timer == null) {
						timer = new Timer();
					}
					timer.schedule(new TimerTask() {
						@Override
						public void run() {
							handler.sendEmptyMessage(UPDATE_TIME);
						}
					}, 0, 1000);
					Toast.makeText(VerificationPhoneActivity.this, "已发送验证码到手机，请查收",
							Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(VerificationPhoneActivity.this, "请输入正确的手机号码",
						Toast.LENGTH_LONG).show();
			}
			
			break;
		}
		case R.id.btnLogin: {
			String num = phoneNum.getText().toString().trim();
			String code = verificationCode.getText().toString().trim();
			if(num.equals("")){
				Toast.makeText(VerificationPhoneActivity.this, "电话号码不能为空",
						Toast.LENGTH_LONG).show();
			}else if(code.equals("")){
				Toast.makeText(VerificationPhoneActivity.this, "请输入验证码",
						Toast.LENGTH_LONG).show();
			}else{
				SharedPreferences.Editor editor = sharedPreferences.edit();
				editor.putBoolean("isVerificated", true);
				editor.commit();
				// TODO 验证输入电话号码是否为空且符合长度，得到验证码与输入进行比较
				startActivity(new Intent(VerificationPhoneActivity.this,
						MainActivity.class));
				finish();
			}
			break;
		}
		}

	}
	
}
